<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/7/5  13:46
 */
namespace app\admin\controller;

class Project extends Common
{
    private $table_admin;

    function __construct()
    {
        parent::__construct();
        $this->table_admin = db('project');
    }

    /*
    项目列表
    */
    public function index()
    {
        $pageParam = ['query' =>[]];
        $where = [];
        $keyword = input('keyword');
        if( $keyword != ''){
            $where['title'] = array('like','%'.input('keyword').'%');
            $pageParam['query']['keyword'] = $keyword;
            $this->assign('keyword', $keyword);
        }

        $article_result = $this->table_admin
            ->where($where)
            ->order('create_time desc')
            ->paginate(2, false, $pageParam)->each(function($item,$key){

                return $item;
            });
//        halt($article_result);
        $page  = $article_result->render();
        $this->assign('page', $page);
        $this->assign('article_result', $article_result);

        return $this->fetch('Project/list');
    }

    //添加项目
    public function add()
    {
        if(request()->isPost()){
            $id = input('id');
            $title = input('title');
            $number = input('number');
            $start_time = strtotime(input('start_time'));
            $finish_time = strtotime(input('finish_time'));
            $expect_time = strtotime(input('expect_time'));
            $user = input('user');
            $partyList = input('partyList/a');
//            halt($partyList);
            $party='';
            if($partyList){
                $party = implode(',', $partyList);
            }

            $content = input('content');
            $mobile = input('mobile');
            $data['title'] = $title;
            $data['number'] = $number;
            $data['start_time'] = $start_time;
            $data['finish_time'] = $finish_time;
            $data['expect_time'] = $expect_time;
            $data['user'] = $user;
            $data['party'] = $party;
            $data['content'] = $content;
            $data['mobile'] = $mobile;
//halt($data);
            if($id){
                //修改
                $bool = $this->table_admin->where('id='.$id)->update($data);
            }else{
                $data['create_time'] = time();
                //添加
                $bool = $this->table_admin->insert($data);
//                $sql = $this->table_admin->getLastSql();
//                $this->success($sql);exit;
            }
            if($bool){
                $this->success('操作成功', url('Project/index'));
            }else{
                $this->error('操作失败');
            }
        }
        return $this->fetch();
    }

    //修改项目
    public function edit()
    {
        $id = input('id');
        $data = $this->table_admin->where('id='.$id)->find();
        $data['party'] = explode(',', $data['party']);
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function info()
    {
        $id = input('id');
        $data = $this->table_admin->where('id='.$id)->find();
        $this->assign('data', $data);
        return $this->fetch();
    }

    //删除项目
    public function delete()
    {
        $id = input("id");
        if(!$id) $this->error('参数有误');
        $da=$this->table_admin->where("id='$id'")->find();
        if(!$da) $this->error('数据不存在');
        $res=$this->table_admin->where("id='$id'")->delete();
        if(!$res) $this->error('删除失败');
        $this->success("删除成功!",url('Project/index'));

    }
}